package com.kob.backend.websocket;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Player {
    private Integer id;
    private Integer botId;  //如果为-1,则为玩家 ，否则为AI Bot
    private String botCode; //Bot的代码
    //每名玩家的起点
    private Integer sx;
    private Integer sy;
    //每名玩家每一步的指令
    private List<Integer> steps;

    private boolean check_tail_increasing(int step){//检验当前回合,蛇的长度是否增加
        if(step <= 10) return true;
        return step % 3 == 1;
    }

    public List<Cell> getCells(){
        List<Cell> cells = new ArrayList<>();
        int[] dx = {-1, 0, 1, 0},dy = {0, 1, 0, -1};
        int x = sx,y = sy;
        int step = 0;
        cells.add(new Cell(x,y));
        for (Integer d : steps) {
            x += dx[d];
            y += dy[d];
            cells.add(new Cell(x,y));
            step ++;//第几回合
            if(!check_tail_increasing(step)){
                cells.remove(0);//蛇尾不增加就把蛇尾删掉
            }
        }
        return cells;
    }

    public String  getStepString(){
        StringBuilder res = new StringBuilder();
        for (Integer step : steps) {
            res.append(step);
        }
        return res.toString();
    }
}
